package org.xgluo.config;
/**
 * Spring 配置类，通过配置类实现
 * 需要通过@EnableTransactionManagement 开启注解事务驱动
 *  开启事务的步骤：
 *      1、定义数据源
 *      2、定义事务管理器
 *      3、定义JDBC执行类
 */

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan(value = "org.xgluo")
@EnableTransactionManagement
public class ApplicationConfig {

    @Autowired
    DBConfig dbConfig;

    @Bean
    DruidDataSource druidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(dbConfig.getMysql_url());
        druidDataSource.setDriverClassName(dbConfig.getMysql_Driver());
        druidDataSource.setUsername(dbConfig.getMysql_user());
        druidDataSource.setPassword(dbConfig.getMysql_pass());
        //druidDataSource.setMaxActive(5);
        return druidDataSource;
    }

    @Bean
    DataSourceTransactionManager dataSourceTransactionManager(){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(druidDataSource());
        return dataSourceTransactionManager;
    }

    @Bean
    JdbcTemplate jdbcTemplate(){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(druidDataSource());
        return jdbcTemplate;
    }



}
